// dateUtils.js

/**
 * 格式化日期
 * @param {Date} date 日期对象
 * @param {string} format 格式字符串，默认为 'yyyy-MM-dd HH:mm:ss'
 * @returns {string} 格式化后的日期字符串
 */
function formatDate(date, format = 'yyyy-MM-dd HH:mm:ss') {
	const map = {
		'yyyy': date.getFullYear(),
		'MM': ('0' + (date.getMonth() + 1)).slice(-2),
		'dd': ('0' + date.getDate()).slice(-2),
		'HH': ('0' + date.getHours()).slice(-2),
		'mm': ('0' + date.getMinutes()).slice(-2),
		'ss': ('0' + date.getSeconds()).slice(-2)
	};
	return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(match) {
		return map[match];
	});
}


function formatDateYMD(dataStr) {
	const date = new Date(dataStr);
	if (!isNaN(date)) {
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0');
		const day = String(date.getDate()).padStart(2, '0');
		const hours = String(date.getHours()).padStart(2, '0');
		const minutes = String(date.getMinutes()).padStart(2, '0');
		const seconds = String(date.getSeconds()).padStart(2, '0');
		return `${year}年${month}月${day}日`;
		// return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
	} else {
		// 无法解析日期，返回原始字符串或抛出错误，取决于需求
		return dataStr;
	}
}

function formatDateYMDHMS(dataStr) {
	const date = new Date(dataStr);
	if (!isNaN(date)) {
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0');
		const day = String(date.getDate()).padStart(2, '0');
		const hours = String(date.getHours()).padStart(2, '0');
		const minutes = String(date.getMinutes()).padStart(2, '0');
		const seconds = String(date.getSeconds()).padStart(2, '0');
		return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
		// return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
	} else {
		// 无法解析日期，返回原始字符串或抛出错误，取决于需求
		return dataStr;
	}
}



// 导出函数
export {
	formatDate,
	formatDateYMD,
	formatDateYMDHMS,
};